package org.database.filter.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.database.filter.model.MucTieu;
import org.database.filter.model.MySynsetWord;
import org.database.filter.model.Nganh;
import org.database.filter.model.NhaDauTuDoanhNghiep;
import org.database.filter.model.QuocGia;
import org.database.filter.model.TuNhan;
import org.database.filter.service.MucTieuService;
import org.database.filter.service.MySynsetWordService;
import org.database.filter.service.NganhService;
import org.database.filter.service.NhaDauTuDoanhNghiepService;
import org.database.filter.service.NhaDauTuService;
import org.database.filter.service.QuocGiaService;
import org.database.filter.service.TextFilterService;
import org.database.filter.service.TextParserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TextFilterServiceImpl implements TextFilterService {

	private static final String MR_KEY_WORD_6 = "1.ông ";

	private static final String MR_KEY_WORD_5 = "1/ông ";

	private static final String MR_KEY_WORD_4 = "ông ";

	private static final String MRS_KEY_WORD_3 = " bà ";

	private static final String MR_KEY_WORD_3 = " ông ";

	private static final String CHI_NHANH_KEY_WORD = "chi nhánh";

	private static final String NATIONALITY_KEY_WORD = "quốc tịch";

	private static final String MRS_KEY_WORD_2 = "/bà";

	private static final String MR_KEY_WORD_2 = "/ông";

	private static final String MRS_KEY_WORD = " bà";

	private static final String MR_KEY_WORD = " ông";

	private static final String CONG_TY_KEY_WORD_2 = " cty";

	private static final String LAP_CONG_TY_KEY_WORD = "lập công ty";

	private static final String CONG_TY_KEY_WORD = "công ty";

	private static final String NGƯNG_KEY_WORD = "ngưng";

	private static final String MUC_TIEU_KEY_WORD = "mt";

	private static final String DOT = "\\.";

	private static final String SEMI_COLON = ";";

	private static final String PHU_GIAO = "H.Phú Giáo";

	private static final String DAU_TIENG = "H.Dầu Tiếng";

	private static final String TAN_VINH_HIEP = "TX.Tân Vĩnh Hiệp";

	private static final String TAN_UYEN = "H.Tân Uyên";

	private static final String BEN_CAT = "H.Bến Cát";

	private static final String THUAN_AN = "TX.Thuận An";

	private static final String DI_AN = "TX.Dĩ An";

	private static final String THU_DAU_MOT = "TP.Thủ Dầu Một";

	private static final String TRONG_NUOC = "Trong Nước";

	private static final String NC_NGOAI = "Nước Ngoài";

	private static final String CO_VON_NUOC_NGOAI = "Có vốn Nước Ngoài";

	private static final String JUS = "jus";

	private static final String STOP = "stop";

	private static final String NEW = "new";

	@Autowired
	private MySynsetWordService synsetWordService;

	@Autowired
	private TextParserService textParserService;

	@Autowired
	private NhaDauTuService nhaDauTuService;

	@Autowired
	private NhaDauTuDoanhNghiepService nhaDauTuDoanhNghiepService;

	@Autowired
	private NganhService nganhService;

	@Autowired
	private MucTieuService mucTieuService;

	@Autowired
	private QuocGiaService quocGiaService;

	private static Map<Integer, String> keyloaihs = new HashMap<Integer, String>();
	private static Map<Integer, String> keytt = new HashMap<Integer, String>();
	private static Map<Integer, String> keyht = new HashMap<Integer, String>();
	private static Map<Integer, String> keyHuyen = new HashMap<Integer, String>();
	private static Map<Integer, String> keyQuocGia = new HashMap<Integer, String>();
	private static Map<Integer, String> keyDN = new HashMap<Integer, String>();
	private static Map<String, List<String>> countryFilter;

	private static Map<Integer, String> getLoaiHSKey() {
		if (keyloaihs.size() == 0) {
			keyloaihs.put(199, NEW);
			keyloaihs.put(200, STOP);
			keyloaihs.put(201, JUS);
		}
		return keyloaihs;
	}

	private static Map<Integer, String> getTinhTrangKey() {
		if (keytt.size() == 0) {
			keytt.put(204, "1");
			keytt.put(205, "2");
			keytt.put(206, "4");
			keytt.put(207, "5");
		}
		return keytt;
	}

	private static Map<Integer, String> getHinhThucDauTuKey() {
		if (keyht.size() == 0) {
			keyht.put(202, CO_VON_NUOC_NGOAI);
			keyht.put(203, NC_NGOAI);
			keyht.put(220, TRONG_NUOC);
		}
		return keyht;
	}

	private static Map<Integer, String> getHuyenKey() {
		if (keyHuyen.size() == 0) {
			keyHuyen.put(208, THU_DAU_MOT);
			keyHuyen.put(209, DI_AN);
			keyHuyen.put(210, THUAN_AN);
			keyHuyen.put(211, BEN_CAT);
			keyHuyen.put(212, TAN_UYEN);
			keyHuyen.put(213, TAN_VINH_HIEP);
			keyHuyen.put(217, DAU_TIENG);
			keyHuyen.put(218, PHU_GIAO);
		}
		return keyHuyen;
	}

	private static Map<Integer, String> getQuocGiaKey() {
		if (keyQuocGia.size() == 0) {
			keyQuocGia.put(1, "Afghanistan");
			keyQuocGia.put(2, "Albania");
			keyQuocGia.put(3, "Algérie");
			keyQuocGia.put(4, "Andorra");
			keyQuocGia.put(5, "Angola");
			keyQuocGia.put(6, "Antigua và Barbuda");
			keyQuocGia.put(7, "Argentina");
			keyQuocGia.put(8, "Armenia");
			keyQuocGia.put(9, "Australia");
			keyQuocGia.put(10, "Áo");
			keyQuocGia.put(11, "Azerbaijan");
			keyQuocGia.put(12, "Bahamas");
			keyQuocGia.put(13, "Bahrain");
			keyQuocGia.put(14, "Bangladesh");
			keyQuocGia.put(15, "Barbados");
			keyQuocGia.put(16, "Belarus");
			keyQuocGia.put(17, "Belgium");
			keyQuocGia.put(18, "Belize");
			keyQuocGia.put(19, "Benin");
			keyQuocGia.put(20, "Bhutan");
			keyQuocGia.put(21, "Bolivia");
			keyQuocGia.put(22, "Bosnia and Herzegovina");
			keyQuocGia.put(23, "Botswana");
			keyQuocGia.put(24, "Brazil");
			keyQuocGia.put(25, "Brunei Darussalam");
			keyQuocGia.put(26, "Bulgaria");
			keyQuocGia.put(27, "Burkina Faso");
			keyQuocGia.put(28, "Burma");
			keyQuocGia.put(29, "Burundi");
			keyQuocGia.put(30, "Campuchia");
			keyQuocGia.put(31, "Cameroon");
			keyQuocGia.put(32, "Canada");
			keyQuocGia.put(33, "Cape Verde");
			keyQuocGia.put(34, "Central African Republic");
			keyQuocGia.put(35, "Chad");
			keyQuocGia.put(36, "Chile");
			keyQuocGia.put(37, "Trung quốc");
			keyQuocGia.put(38, "Colombia");
			keyQuocGia.put(39, "Comoros");
			keyQuocGia.put(40, "Republic of Congo");
			keyQuocGia.put(41, "Democratic Republic of the Congo");
			keyQuocGia.put(42, "Costa Rica");
			keyQuocGia.put(43, "Côte d'Ivoire");
			keyQuocGia.put(44, "Croatia");
			keyQuocGia.put(45, "Cuba");
			keyQuocGia.put(46, "Cyprus");
			keyQuocGia.put(47, "cộng hòa séc");
			keyQuocGia.put(48, "Denmark");
			keyQuocGia.put(49, "Djibouti");
			keyQuocGia.put(50, "Dominica");
			keyQuocGia.put(51, "Dominican Republic");
			keyQuocGia.put(52, "Ecuador");
			keyQuocGia.put(53, "Ai Cập");
			keyQuocGia.put(54, "El Salvador");
			keyQuocGia.put(55, "Equatorial Guinea");
			keyQuocGia.put(56, "Eritrea");
			keyQuocGia.put(57, "Estonia");
			keyQuocGia.put(58, "Ethiopia");
			keyQuocGia.put(59, "Fiji");
			keyQuocGia.put(60, "Phần Lan");
			keyQuocGia.put(61, "Pháp");
			keyQuocGia.put(62, "Gabon");
			keyQuocGia.put(63, "Gambia");
			keyQuocGia.put(64, "Georgia");
			keyQuocGia.put(65, "Đức");
			keyQuocGia.put(66, "Ghana");
			keyQuocGia.put(67, "Greece");
			keyQuocGia.put(68, "Grenada");
			keyQuocGia.put(69, "Guatemala");
			keyQuocGia.put(70, "Guinea");
			keyQuocGia.put(71, "Guinea-Bissau");
			keyQuocGia.put(72, "Guyana");
			keyQuocGia.put(73, "Haiti");
			keyQuocGia.put(74, "Honduras");
			keyQuocGia.put(75, "Hungary");
			keyQuocGia.put(76, "Iceland");
			keyQuocGia.put(77, "Ấn Độ");
			keyQuocGia.put(78, "Indonesia");
			keyQuocGia.put(79, "Ba Tư");
			keyQuocGia.put(80, "Iraq");
			keyQuocGia.put(81, "Ireland");
			keyQuocGia.put(82, "Israel");
			keyQuocGia.put(83, "Ý");
			keyQuocGia.put(84, "Jamaica");
			keyQuocGia.put(85, "Nhật Bản");
			keyQuocGia.put(86, "Jordan");
			keyQuocGia.put(87, "Kazakhstan");
			keyQuocGia.put(88, "Kenya");
			keyQuocGia.put(88, "Kiribati");
			keyQuocGia.put(90, "Bắc Triều Tiên");
			keyQuocGia.put(91, "Đại Hàn");
			keyQuocGia.put(92, "Kuwait");
			keyQuocGia.put(93, "Kyrgyzstan");
			keyQuocGia.put(94, "Lào");
			keyQuocGia.put(95, "Latvia");
			keyQuocGia.put(96, "Lebanon");
			keyQuocGia.put(97, "Lesotho");
			keyQuocGia.put(98, "Liberia");
			keyQuocGia.put(99, "Libya");
			keyQuocGia.put(100, "Liechtenstein");
			keyQuocGia.put(101, "Lithuania");
			keyQuocGia.put(102, "Luxembourg");
			keyQuocGia.put(103, "Macedonia");
			keyQuocGia.put(104, "Madagascar");
			keyQuocGia.put(105, "Malawi");
			keyQuocGia.put(106, "Malaysia");
			keyQuocGia.put(107, "Maldives");
			keyQuocGia.put(108, "Mali");
			keyQuocGia.put(109, "Malta");
			keyQuocGia.put(110, "Marshall Islands");
			keyQuocGia.put(111, "Mauritania");
			keyQuocGia.put(112, "Mauritius");
			keyQuocGia.put(113, "Mexico");
			keyQuocGia.put(114, "Micronesia");
			keyQuocGia.put(115, "Moldova");
			keyQuocGia.put(116, "Monaco");
			keyQuocGia.put(117, "Mông Cổ");
			keyQuocGia.put(118, "Montenegro");
			keyQuocGia.put(119, "Morocco");
			keyQuocGia.put(120, "Mozambique");
			keyQuocGia.put(121, "Myanmar");
			keyQuocGia.put(122, "Namibia");
			keyQuocGia.put(123, "Nauru");
			keyQuocGia.put(124, "Nepal");
			keyQuocGia.put(125, "Hà Lan");
			keyQuocGia.put(126, "Tân Tây Lan");
			keyQuocGia.put(127, "Nicaragua");
			keyQuocGia.put(128, "Niger");
			keyQuocGia.put(129, "Nigeria");
			keyQuocGia.put(130, "Na Uy");
			keyQuocGia.put(131, "Oman");
			keyQuocGia.put(132, "Pakistan");
			keyQuocGia.put(133, "Palau");
			keyQuocGia.put(134, "Palestinian State");
			keyQuocGia.put(135, "Panama");
			keyQuocGia.put(136, "Papua New Guinea");
			keyQuocGia.put(137, "Paraguay");
			keyQuocGia.put(138, "Peru");
			keyQuocGia.put(139, "Philippines");
			keyQuocGia.put(140, "Ba Lan");
			keyQuocGia.put(141, "Bồ Đào Nha");
			keyQuocGia.put(142, "Qatar");
			keyQuocGia.put(143, "Romania");
			keyQuocGia.put(144, "Nga");
			keyQuocGia.put(145, "Rwanda");
			keyQuocGia.put(146, "Kitts and Nevis");
			keyQuocGia.put(147, "Lucia");
			keyQuocGia.put(148, "Vincent and The Grenadines");
			keyQuocGia.put(149, "Samoa");
			keyQuocGia.put(150, "San Marino");
			keyQuocGia.put(151, "São Tomé and Príncipe");
			keyQuocGia.put(152, "Saudi Arabia");
			keyQuocGia.put(153, "Senegal");
			keyQuocGia.put(154, "Serbia");
			keyQuocGia.put(155, "Seychelles");
			keyQuocGia.put(156, "Sierra Leone");
			keyQuocGia.put(157, "Singapore");
			keyQuocGia.put(158, "Slovakia");
			keyQuocGia.put(159, "Slovenia");
			keyQuocGia.put(160, "Solomon Islands");
			keyQuocGia.put(161, "Somalia");
			keyQuocGia.put(162, "Nam Phi");
			keyQuocGia.put(163, "Spain");
			keyQuocGia.put(164, "Sri Lanka");
			keyQuocGia.put(165, "Sudan");
			keyQuocGia.put(166, "Suriname");
			keyQuocGia.put(167, "Swaziland");
			keyQuocGia.put(168, "Thụy Điển");
			keyQuocGia.put(169, "Thụy Sĩ");
			keyQuocGia.put(170, "Syria");
			keyQuocGia.put(171, "Đài Loan");
			keyQuocGia.put(172, "Tajikistan");
			keyQuocGia.put(173, "Tanzania");
			keyQuocGia.put(174, "Thái Lan");
			keyQuocGia.put(175, "Togo");
			keyQuocGia.put(176, "Tonga");
			keyQuocGia.put(177, "Trinidad and Tobago");
			keyQuocGia.put(178, "Tunisia");
			keyQuocGia.put(179, "Thổ Nhĩ Kỳ");
			keyQuocGia.put(180, "Turkmenistan");
			keyQuocGia.put(181, "Tuvalu");
			keyQuocGia.put(182, "Uganda");
			keyQuocGia.put(183, "Ukraine");
			keyQuocGia.put(184, "United Arab Emirates");
			keyQuocGia.put(185, "Anh");
			keyQuocGia.put(186, "Mỹ");
			keyQuocGia.put(187, "Uruguay");
			keyQuocGia.put(188, "Uzbekistan");
			keyQuocGia.put(189, "Vanuatu");
			keyQuocGia.put(190, "Vatican City");
			keyQuocGia.put(191, "Venezuela");
			keyQuocGia.put(192, "Việt Nam");
			keyQuocGia.put(193, "Western Sahara");
			keyQuocGia.put(194, "Republic of Yemen");
			keyQuocGia.put(195, "Yugoslavia");
			keyQuocGia.put(196, "Zaire");
			keyQuocGia.put(197, "Zambia");
			keyQuocGia.put(198, "Zimbabwe");
		}
		return keyQuocGia;
	}

	private static Map<Integer, String> getLoaiHinhDoanhNghiepKey() {
		if (keyDN.size() == 0) {
			keyDN.put(214, "CTy Cổ Phần");
			keyDN.put(215, "Chi Nhánh");
			keyDN.put(216, "Cty Nhà Nước");
			keyDN.put(219, "Dự Án Độc Lập");
		}
		return keyDN;
	}

	private Map<String, List<String>> getFilter(Map<Integer, String> keys) {
		Map<String, List<String>> filter = new HashMap<String, List<String>>();
		for (Entry<Integer, String> pair : keys.entrySet()) {
			Integer synset_id = pair.getKey();
			List<MySynsetWord> synsetWord = synsetWordService
					.getSynsetWordBySynsetId(synset_id);
			List<String> words = new ArrayList<String>();
			for (MySynsetWord word : synsetWord) {
				words.add(word.getWord());
			}
			filter.put(pair.getValue(), words);
		}
		return filter;
	}

	@Override
	public List<Integer> doFilterMucTieu(String input) {
		List<Integer> muctieu = new ArrayList<Integer>();
		if (input == null || input.trim().isEmpty()) {
			return null;
		}
		input = input.toLowerCase();
		List<String> mts = new ArrayList<String>();
		String[] mtArray;
		/*if(!isVsip){
			mtArray = input.split(SEMI_COLON);
			for(String mt : mtArray) {
				for(String mt2 : mt.split(DOT)) {
					mts.add(mt2);
				}
			}
		} else {
			//mtArray = input.split(MUC_TIEU_KEY_WORD);
			mtArray = input.split(SEMI_COLON);
			for(String mt : mtArray) {
				for(String mt2 : mt.split(DOT)) {
					mts.add(mt2);
				}
			}
		}*/
		mtArray = input.split(SEMI_COLON);
		for(String mt : mtArray) {
			for(String mt2 : mt.split(DOT)) {
				mts.add(mt2);
			}
		}
		for (int i = 0; i < mts.size(); i++) {
			String mt = "";
			if (i != 0) {
				/*if(isVsip && mts.get(i).length()>=3){
					//mt = mts.get(i).substring(2).replace(SEMI_COLON, "");
					mt = mts.get(i);
				} else {
					mt = mts.get(i);
				}*/
				mt = mts.get(i);
				if (mt != null && !mt.trim().isEmpty()) {
					MucTieu mtieu = new MucTieu();
					mtieu.setMUC_TIEU(mt);
					muctieu.add(mucTieuService.saveOrUpdate(mtieu));
				}
			} else {
				mt = mts.get(i).replace("'", "");
				if (mt != null && !mt.trim().isEmpty()) {
					MucTieu mtieu = new MucTieu();
					mtieu.setMUC_TIEU(mt);
					muctieu.add(mucTieuService.saveOrUpdate(mtieu));
				}
			}
		}
		return muctieu;
	}

	public String doFilterLoaiHS(String input) {
		if (input == null || input.trim().isEmpty()) {
			return null;
		}
		input = input.toLowerCase();
		for (Entry<String, List<String>> pair : getFilter(getLoaiHSKey())
				.entrySet()) {
			List<String> keyWords = pair.getValue();

			for (String keyWord : keyWords) {
				if (input.contains(keyWord)) {
					return pair.getKey();
				}
			}

		}
		return input;
	}

	public String doFilterTinhTrang(String input) {
		if (input == null || input.trim().isEmpty()) {
			return null;
		}
		input = input.toLowerCase();
		if (input.contains(NGƯNG_KEY_WORD)) {
			return "4";
		}
		for (Entry<String, List<String>> pair : getFilter(getTinhTrangKey())
				.entrySet()) {
			List<String> keyWords = pair.getValue();
			for (String keyWord : keyWords) {
				if (input.contains(keyWord)) {
					return pair.getKey();
				}
			}
		}
		return input;
	}

	public String doFilterHinhThuc(String input) {
		if (input == null || input.trim().isEmpty()) {
			return null;
		}
		input = input.toLowerCase();
		for (Entry<String, List<String>> pair : getFilter(getHinhThucDauTuKey())
				.entrySet()) {
			List<String> keyWords = pair.getValue();
			for (String keyWord : keyWords) {
				if (input.contains(keyWord)) {
					return pair.getKey();
				}
			}

		}
		return input;
	}

	public String doFilterHuyen(String input) {
		if (input == null || input.trim().isEmpty()) {
			return null;
		}
		input = input.toLowerCase();
		for (Entry<String, List<String>> pair : getFilter(getHuyenKey())
				.entrySet()) {
			List<String> keyWords = pair.getValue();
			for (String keyWord : keyWords) {
				if (input.contains(keyWord)) {
					return pair.getKey();
				}
			}

		}
		return input;
	}

	public String doFilterQuocGia(String input) {
		if (input == null || input.trim().isEmpty()) {
			return null;
		}
		input = input.toLowerCase();
		if (countryFilter == null) {
			countryFilter = getFilter(getQuocGiaKey());
		}
		for (Entry<String, List<String>> pair : countryFilter.entrySet()) {
			List<String> keyWords = pair.getValue();
			for (String keyWord : keyWords) {
				if (input.contains(keyWord)) {
					return pair.getKey();
				}
			}

		}
		return input;
	}

	public String doFilterLoaiHinhDoanhNghiep(String input) {
		if (input == null || input.trim().isEmpty()) {
			return null;
		}
		input = input.toLowerCase();
		if (input.contains("trở lên") || input.contains("hai")
				|| input.contains("2")) {
			return "CTy TNHH Hai Thành Viên Trở Lên";
		}
		if (input.contains("một") || input.contains("tnhh")
				|| input.contains("1")) {
			return "CTy TNHH Một Thành Viên";
		}
		for (Entry<String, List<String>> pair : getFilter(
				getLoaiHinhDoanhNghiepKey()).entrySet()) {
			List<String> keyWords = pair.getValue();
			for (String keyWord : keyWords) {
				if (input.contains(keyWord)) {
					return pair.getKey();
				}
			}
		}
		return input;
	}

	@Override
	public List<Long> doFilterNganhNghe(String input) {
		List<Long> nganhNghe = new ArrayList<Long>();
		if (input == null || input.trim().isEmpty()) {
			return null;
		}
		input = input.toLowerCase();
		String[] nganhArray;
		List<String> nganhs = new ArrayList<String>();
		/*if(!isVsip){
			nganhArray = input.split(SEMI_COLON);
			for(String nganh : nganhArray) {
				for(String nganh2 : nganh.split(DOT)) {
					nganhs.add(nganh2);
				}
			}
		} else {
			nganhArray = input.split("nn");
		}*/
		nganhArray = input.split(SEMI_COLON);
		for(String nganh : nganhArray) {
			for(String nganh2 : nganh.split(DOT)) {
				nganhs.add(nganh2);
			}
		}
		for (int i = 0; i < nganhs.size(); i++) {
			String nganh = "";
			if (i != 0) {
				/*if(isVsip && nganhs.get(i).length()>=3){
					nganh = nganhs.get(i).substring(2).replace(SEMI_COLON, "");
				} else {
					nganh = nganhs.get(i);
				}*/
				nganh = nganhs.get(i);
				Pattern pp = Pattern.compile("-?\\d+");
				Matcher m = pp.matcher(nganh);
				if (m.find()) {
					long key = Long.parseLong(m.group());
					Nganh ng = nganhService.getNganhById(key);
					if (ng == null) {
						Nganh n = new Nganh();
						n.setMA_NGANH(key);
						n.setTEN_NGANH(nganh);
						nganhNghe.add(nganhService.saveOrUpdate(n));
					} else {
						nganhNghe.add(ng.getMA_NGANH());
					}
				} else {
					Nganh n = new Nganh();
					if (nganh != null && !nganh.trim().isEmpty()) {
						n.setTEN_NGANH(nganh);
						nganhNghe.add(nganhService.saveOrUpdate(n));
					}
				}
			} else {
				nganh = nganhs.get(i).replace("'", "");
				if (nganh != null && !nganh.trim().isEmpty()) {
					Nganh n = new Nganh();
					n.setTEN_NGANH(nganh);
					nganhNghe.add(nganhService.saveOrUpdate(n));
				}
			}
		}
		return nganhNghe;
	}

	@Override
	public List<NhaDauTuDoanhNghiep> doFilterDauTuDoanhNghiep(String input,
			Integer ma_cq) {
		if (input == null) {
			return null;
		}
		input = input.toLowerCase();
		List<NhaDauTuDoanhNghiep> list = new ArrayList<NhaDauTuDoanhNghiep>();
		int begin = 1;
		if (!input.contains(CONG_TY_KEY_WORD)) {
			begin = 0;
		}
		String[] listCT = input.replace(LAP_CONG_TY_KEY_WORD, "").split(CONG_TY_KEY_WORD);

		for (int i = begin; i < listCT.length; i++) {
			String dn = listCT[i];
			if (dn.contains(CONG_TY_KEY_WORD_2)) {
				for (String dnn : dn.split(CONG_TY_KEY_WORD_2)) {
					NhaDauTuDoanhNghiep ndtdn = textParserService
							.parseTextDN(dnn, ma_cq);
					if (ndtdn != null && ndtdn.getTENDN() != null
							&& !ndtdn.getTENDN().trim().isEmpty()) {
						ndtdn.setID_DN(nhaDauTuDoanhNghiepService.saveOrUpdate(ndtdn));
						list.add(ndtdn);
					}
				}
			} else {
				NhaDauTuDoanhNghiep ndtdn = textParserService
						.parseTextDN(dn, ma_cq);
				if (ndtdn != null && ndtdn.getTENDN() != null
						&& !ndtdn.getTENDN().trim().isEmpty()) {
					ndtdn.setID_DN(nhaDauTuDoanhNghiepService.saveOrUpdate(ndtdn));
					list.add(ndtdn);
				}
			}

		}
		return list;
	}

	@Override
	public List<TuNhan> doFilterDauTuCaNhan(String input, Integer ma_cq, String address) {
		if (input == null) {
			return null;
		}
		String[] addresses = null;
		if(address != null && address.trim().isEmpty()) {
			addresses = address.split(DOT);
		}
		int addressIndex = 0;
		input = input.toLowerCase();
		List<TuNhan> list = new ArrayList<TuNhan>();
		int begin = 1;
		if (!input.contains(MR_KEY_WORD)) {
			begin = 0;
		}
		String[] listNDT = input.split(MR_KEY_WORD);
		for (int i = begin; i < listNDT.length; i++) {
			String ndt = listNDT[i];
			if (ndt.contains(MRS_KEY_WORD)) {
				for (String nt : ndt.split(MRS_KEY_WORD)) {
					String ad = "";
					if(addresses != null && addressIndex < addresses.length) {
						ad = addresses[addressIndex];
						addressIndex++;
					}
					list = doParseTextTuNhan(list, nt, ma_cq, ad);
				}
			} else if (ndt.contains(MR_KEY_WORD_2)) {				
				for (String nt : ndt.split(MR_KEY_WORD_2)) {
					if (nt.contains(MRS_KEY_WORD_2)) {
						for (String ntt : nt.split(MRS_KEY_WORD_2)) {
							String ad = "";
							if(addresses != null && addressIndex < addresses.length) {
								ad = addresses[addressIndex];
								addressIndex++;
							}
							list = doParseTextTuNhan(list, ntt, ma_cq, ad);
						}
					} else {
						String ad = "";
						if(addresses != null && addressIndex < addresses.length) {
							ad = addresses[addressIndex];
							addressIndex++;
						}
						list = doParseTextTuNhan(list, nt, ma_cq, ad);
					}
				}
			} else {
				String ad = "";
				if(addresses != null && addressIndex < addresses.length) {
					ad = addresses[addressIndex];
					addressIndex++;
				}
				list = doParseTextTuNhan(list, ndt, ma_cq, ad);
			}
		}
		return list;
	}

	private List<TuNhan> doParseTextTuNhan(List<TuNhan> list, String nt,
			Integer ma_cq, String ad) {
		TuNhan ndtdn = textParserService.parseTextPerson(nt, ma_cq, ad);
		if (ndtdn != null && ndtdn.getTEN() != null
				&& !ndtdn.getTEN().trim().isEmpty()) {
			if (ndtdn.getHO_KHAU() != null) {
				QuocGia qg = new QuocGia();
				qg.setTEN_QUOC_GIA(ndtdn.getHO_KHAU().replace(NATIONALITY_KEY_WORD, ""));
				ndtdn.setHO_KHAU(String.valueOf(quocGiaService.saveOrUpdate(qg)));
			}
		
			nhaDauTuService.saveOrUpdate(ndtdn);
			list.add(ndtdn);
		}
		return list;
	}

	@Override
	public boolean isDoanhNghiep(String input) {
		if (input == null || input.trim().isEmpty()) {
			return false;
		}
		input = input.toLowerCase();
		boolean result = input.contains(CONG_TY_KEY_WORD)
				|| input.contains(CHI_NHANH_KEY_WORD);
		if (!result) {
			result = !(input.contains(MR_KEY_WORD_3) || input.contains(MRS_KEY_WORD_3)
					|| input.startsWith(MR_KEY_WORD_4) || input.startsWith(MR_KEY_WORD_5) || input
					.startsWith(MR_KEY_WORD_6));
		}
		return result;
	}
}
